package dao.impl;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import utils.DBGetDataUtil;
import utils.Pub;

import java.sql.SQLException;

public class DataStatisticsOperation {
	public static final Logger logger = LogManager.getLogger(DataStatisticsOperation.class.getName());

	public JSONObject getJsonResultByDateAndSersors(String warning, String bridgeType, String beginDate, String endDate,
			String sersors, String dataTable) throws Exception {
		JSONObject result = new JSONObject();
		JSONArray resultArray = new JSONArray();
		String[] sersorArray = sersors.split(",");
		String sql = null;
		try {
			for (String aSersorArray : sersorArray) {
				sql = "select TIME,DATA from " + dataTable
						+ "  where TIME between ? and ? AND SERSORS = ? order by TIME";
				JSONObject data = DBGetDataUtil.getJsonData(warning, sql, dataTable, beginDate, endDate, aSersorArray);
				data.put(Pub.SENSORID, aSersorArray);
				resultArray.add(data);
			}
		} catch (SQLException e) {
			result.put(Pub.STATUS, false);
			throw new Exception(e.getMessage());
		}
		result.put("type", bridgeType);
		result.put(Pub.STATUS, true);
		result.put(Pub.DATA, resultArray);
		return result;
	}

	public JSONObject getSensorsdata(String tableName, String bridgeType, String dataType) {
		JSONObject result = new JSONObject();
		String sql = "select  SERSORS  from " + tableName + " group by SERSORS";
		logger.info(sql);
		JSONArray data = new JSONArray();
		try {
			data = DBGetDataUtil.getSensors(sql);
		} catch (Exception e) {
			e.printStackTrace();
		}
		result.put(Pub.STATUS, true);
		result.put("bridgeType", bridgeType);
		result.put("dataType", dataType);
		result.put(Pub.DATA, data);
		return result;
	}

}
